<?php
class studentModel extends model
{
    public function create()
    {
        try{
            $student = fixer::input('post')
                ->specialchars('campus, student, sex, school, class, grade, comment')
                ->add('date', date('Y-m-d H:i:s'))
                ->get();
            $base = new stdClass();
            $base->student = $student->student;
            $base->sex = $student->sex;
            $base->comment = $student->comment;
            $base->date = $student->date;
            $this->dao->begin();
            $this->dao
                ->insert('student')
                ->data($base)
                ->batchCheck('campus, student, school, class, grade', 'notempty')
                ->exec();
            $bool1 = $this->dao->lastInsertID();
            $campus = new stdClass();
            $studentId = $bool1;
            $campus->student_id = $studentId;
            $campus->campus_id = $student->campus;
            $campus->school = $student->school;
            $campus->class = $student->class;
            $campus->grade = $student->grade;
            $this->dao
                ->insert('student_campus')
                ->data($campus)
                ->batchCheck('campus, student, school, class, grade, comment', 'notempty')
                ->exec();
            $bool2 = $this->dao->lastInsertID();
            if ($bool1 && $bool2) {
                $this->dao->commit();
                return $bool2;
            } else {
                $this->dao->rollback();
                return false;
            }
        } catch (Exception $e) {
            // 开发时打印出异常，线上写日志！
            var_dump($e->getMessage());
        }
    }
    public function delete($id, $table = null)
    {
        $this->dao->update('student')->set("del = 1")->where('id')->eq($id)->exec();
    }
    public function getList($pager = null)
    {
        return $this->dao->select("a.*,b.school,b.class,b.grade,c.name as campus")
            ->from('student')->alias('a')
            ->leftJoin('student_campus')->alias('b')
            ->on('a.id=b.student_id')
            ->leftJoin('campus')->alias('c')
            ->on("b.campus_id=c.id")
            ->where("a.del")->ne("1")
            ->orderBy('a.id asc')
            ->page($pager)
            ->fetchAll();
    }
    public function getById($id)
    {
        return $this->dao->select("a.*,b.campus_id as campusId,b.school,b.class,b.grade,c.name as campus")
            ->from('student')->alias('a')
            ->leftJoin('student_campus')->alias('b')
            ->on('a.id=b.student_id')
            ->leftJoin('campus')->alias('c')
            ->on("b.campus_id=c.id")
            ->where("a.id={$id}")
            ->orderBy('a.id asc')
            ->fetch();
    }
    public function update($studentId)
    {
        try {
            $student = fixer::input('post')
                ->specialchars('campus, student, sex, school, class, grade, comment')
                ->get();
            $base = new stdClass();
            $base->student = $student->student;
            $base->sex = $student->sex;
            $base->comment = $student->comment;
            $this->dao->begin();
            $bool1 = $this->dao->update('student')
                ->data($base)
                ->where('id')
                ->eq($studentId)
                ->exec();
            $campus = new stdClass();
            $campus->campus_id = $student->campus;
            $campus->school = $student->school;
            $campus->class = $student->class;
            $campus->grade = $student->grade;
            $bool2 = $this->dao->update('student_campus')
                ->data($campus)
                ->where('student_id')
                ->eq($studentId)
                ->exec();
            if (is_int($bool1) && is_int($bool2)) {
                $this->dao->commit();
                return true;
            } else {
                $this->dao->rollback();
                return false;
            }
        } catch (Exception $e) {
            // 开发时打印出异常，线上写日志！
            var_dump($e->getMessage());
        }
    }
    public function  getaccount($studentId)
    {
        return $this->dao->select("remainder")
            ->from('student_account')
            ->where('student_id')->eq($studentId)
            ->fetch();
    }
    public function lsCampus()
    {
        $campusList = $this->dao->select("id,name")
            ->from('campus')
            ->where('del')->ne('1')
            ->orderBy('id asc')
            ->fetchAll();
        $temp = [];
        foreach ($campusList as $campus) {
            $temp[$campus->id] = $campus->name;
        }
        return $temp;
    }
    public function lsAccountClass()
    {
        $accountClassList = $this->dao->select("type,name")
            ->from('account_class')
            ->orderBy('type asc')
            ->fetchAll();
        $temp = [];
        foreach ($accountClassList as $accountClass) {
            $temp[$accountClass->type] = $accountClass->name;
        }
        return $temp;
    }
    public function charge()
    {
        $charge = fixer::input('post')
            ->specialchars('account_type,student_id, money')
            ->add('date', date('Y-m-d H:i:s'))->get();
        $studentAccount = new stdClass();
        $studentAccount->remainder = $charge->money;
        $studentAccount->student_id = $charge->student_id;
        $studentAccount->account_type= $charge->account_type;
        $hasOne = $this->dao->select('student_id,remainder')
            ->from('student_account')
            ->where("student_id")->eq($charge->student_id)
            ->andWhere('account_type')->eq($charge->account_type)
            ->fetch();
        if ($hasOne == false) {
            // 插入新的一条余额数据
            $this->dao
                ->insert("student_account")
                ->data($studentAccount)
                ->exec();
        } else {
            // 累加余额
            $studentAccount->remainder += $hasOne->remainder;
            $this->dao
                ->update("student_account")
                ->data($studentAccount)
                ->where("student_id")->eq($charge->student_id)
                ->andWhere('account_type')->eq($charge->account_type)
                ->exec();
        }
        return $this->dao
            ->insert("student_charge_log")
            ->data($charge)
            ->exec();
    }
}